﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using sonicERPDAL;
using sonicERPinfo;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace sonicERPinterface.Charp
{
    [WebService(Namespace = "sonicERPinterfaceCharp")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


    [System.Web.Script.Services.ScriptService]
    public class dal : System.Web.Services.WebService
    {
        ClienteDAL clientedal = new ClienteDAL();
        Tabelainfo info = new Tabelainfo();

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string retornarclientes(string _criterio)
        {
            //    string stringConexao = WebConfigurationManager.ConnectionStrings["sonicerp"].ConnectionString;
            //    SqlConnection conexao = new SqlConnection(stringConexao);
            //    string select = "SELECT NOME FROM FORNECEDOR_HISTORICO WHERE NOME  LIKE '%' + '" + _criterio + "' + '%' ORDER BY NOME ASC";
            //    SqlCommand cmd = new SqlCommand(select, conexao);
            //    conexao.Open();

            //    List<string> lstProdutos = new List<string>();

            //    SqlDataReader registro = cmd.ExecuteReader();

            //    while (registro.Read())
            //    {
            //        lstProdutos.Add((string)registro["NOME"]);
            //    }

            //    return JsonConvert.SerializeObject(lstProdutos, Formatting.Indented);

            //}

            //    info.cliente.nomecliente = _criterio;
            //    DataView dv = clientedal.pesquisanomecliente(info);
            //        string [,] lista = new string [dv.Table.Rows.Count,dv.Table.Columns.Count];
            //              int colum = 0;


            //            foreach (DataRow item in dv.Table.Rows)
            //            {
            //                if (colum == dv.Table.Columns.Count)
            //                {
            //                    return JsonConvert.SerializeObject(lista, Formatting.Indented);
            //                }
            //                while (colum < dv.Table.Columns.Count)
            //                {

            //                    for (int row = 0; row < dv.Table.Rows.Count; row++)
            //                    {

            //                        lista[row, colum] = item[colum].ToString();
            //                    }
            //                    colum++;
            //                }
            //            }


            //    return JsonConvert.SerializeObject(lista, Formatting.Indented);

            //}
            info.cliente.nomecliente = _criterio;
            DataView dv = new DataView();

            List<string> lista = new List<string>();
            foreach (DataRow item in dv.Table.Rows)
            {
                lista.Add(item[0].ToString());
            }
            return JsonConvert.SerializeObject(lista, Formatting.Indented);
        }
    }
}


